package com.chenx;

import org.apache.catalina.startup.Tomcat;

/**
 * TODO
 * 实现tomcat启动
 * @author chenx
 * @date 2021/6/16
 */
public class SpringApplication {

    public static void run(Class<?> primarySource, String... args) {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8081);
        System.out.println("tomcat start ");
        String docBase = SpringApplication.class.getResource("/").getPath();
        System.out.println(docBase);
        try {
//            //上下文
//            // 设置contextPath和路径
            //非常重要当项目路径添加到tomcat中
            tomcat.addWebapp("/", docBase);
            tomcat.start();
            //没有这一步，tomcat启动之后main退出
            tomcat.getServer().await();
            System.out.println("tomcat start end");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
